/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "./DirectX9Private.h"

#include "alkes/directx9/DirectXRenderTarget.h"


namespace alkes {

DirectXRenderTarget* DirectXRenderTarget::createBackSurface(DirectXGraphics& directx)
{
    TargetBehavior target_behavior;
    {
        const D3DPRESENT_PARAMETERS& pre_param = directx.getPresentParameters();
        switch (pre_param.SwapEffect)
        {
        case D3DSWAPEFFECT_DISCARD:
            target_behavior = AL_BEHAVIOR_DISCARD;
            break;
        case D3DSWAPEFFECT_COPY:
            /* TODO: Research copy behavior.
            the content of the back buffer is changed in some environment.
            */
            //target_behavior = AL_BEHAVIOR_SINGLE;
            target_behavior = AL_BEHAVIOR_DISCARD;
            break;
        case D3DSWAPEFFECT_FLIP:
            if (pre_param.BackBufferCount == 1)
                target_behavior = AL_BEHAVIOR_DOUBLE;
            else if (pre_param.BackBufferCount == 2)
                target_behavior = AL_BEHAVIOR_TRIPLE;
            else
                return 0;
            break;
        default:
            return 0;
        }
    }

    URect rect(0, 0,
        (Rect::value_type)directx.getPresentParameters().BackBufferWidth,
        (Rect::value_type)directx.getPresentParameters().BackBufferHeight);
    DirectXRenderTarget* target = new DirectXRenderTarget(rect, target_behavior);

    return target;
}

void DirectXRenderTarget::destroy()
{
    delete this;
}

DirectXRenderTarget::DirectXRenderTarget(const Rect& rect, TargetBehavior target_behavior)
: RenderTarget(target_behavior, rect.width, rect.height)
, render_target_(0)
{
}

DirectXRenderTarget::DirectXRenderTarget(IDirect3DSurface* target, TargetBehavior target_behavior)
: RenderTarget(target_behavior, 0, 0)
, render_target_(target)
{
    if (target)
    {
        D3DSURFACE_DESC desc;
        target->GetDesc(&desc);
        updateSize((uint16_t)desc.Width, (uint16_t)desc.Height);
    }
}

DirectXRenderTarget::~DirectXRenderTarget()
{
    releaseSurface();
}

void DirectXRenderTarget::releaseSurface()
{
    if (render_target_)
    {
        render_target_->Release();
        render_target_ = 0;
    }
}

void DirectXRenderTarget::setSurface(IDirect3DSurface* new_surface)
{
    releaseSurface();
    render_target_ = new_surface;
}

IDirect3DSurface* DirectXRenderTarget::getSurface()
{
    return render_target_;
}

void DirectXRenderTarget::onResetDevice()
{
}

}
